import useStore from '@/stores';


const storageList = ['accessToken', 'VisitedViews', 'userInfo']

//区分刷新和关闭浏览器  刷新数据恢复
window.addEventListener('load', () => {

  if (!localStorage.getItem(storageList[0]) && sessionStorage.getItem(storageList[0])) {
    storageList.forEach(item => {
      let info = sessionStorage.getItem(item)
      info = info === 'null' ? JSON.parse(info) : info
      localStorage.setItem(item, info)
    })

  }
})

// 刷新或关闭浏览器 刷新存数据到session
window.addEventListener('beforeunload', () => {
  const { userStore, routerStore } = useStore()

  storageList.forEach(item => {
    const info = localStorage.getItem(item)
    sessionStorage.setItem(item, info)

  })

  userStore.removeToken()
  userStore.removeUserInfo()
  routerStore.removeLocalStorageVisitedViews()

})